import Vue from 'vue'
import axios from 'axios'
import {message} from 'element-ui'

axios.defaults.baseURL = "http://127.0.0.1:3000/api"

//由于后端有token校验，所以采用request拦截器，对所有的axios请求添加token校验
axios.interceptors.request.use(
    function(config){
        //请求头携带token的键，固定写法authorization  
        var token = localStorage.getItem("userData")
        if(token != undefined){
            const userData = JSON.parse(localStorage.getItem("userData"))||{};      
            config.headers.authorization = userData.token
        }
        return config
    }
)

axios.interceptors.response.use(
    function(response){
        if(response.data.code == 403){
            location.href = "#/login"
        }
        return response
    }
)

Vue.prototype.$http = axios 
export default axios